<?php


namespace app\common\helpers;


class ListHelper
{
    public static function getRatio($list, $column = 'value', $index_key = 'name', $index_rate = 'ratio', $precision = 2)
    {
        $total = array_sum(array_column($list, $column, $index_key));


        foreach ($list as $key => $item) {

            $list[$key][$index_rate] = $total == 0 ? 0 : round(($item[$column] / $total) * 100, $precision);
        }

        return $list;
    }

    public static function getRound($num, $total, $decimal = 2, $value = 0)
    {
        if (is_numeric($total) == false) return $num;

        if ($total == 0) return $value;

        $ratio = $num > 0 ? bcdiv($num, $total, 4) : 0;

        $ratio_round = round(bcmul($ratio, 100, 2), $decimal);

        return $ratio_round;
    }

    /**
     * @param $workExperiences
     * @param $value
     * @return array
     *  获取一个二维数组中最大的数组和最小的数组
     */
    public static function getMaxMinArray($data, $value)
    {
        $max = 0;
        $min = 0;
        if ($data) {
            $quitDates = array_column($data, $value);
            $maxItemKey = array_search(max($quitDates), $quitDates);
            $minItemKey = array_search(min($quitDates), $quitDates);
            $max = $data[$maxItemKey];
            $min = $data[$minItemKey];

        }

        return [$max, $min];
    }

    /**
     * @param $data
     * @param $lastData
     * @param int $precision
     * @return float|int
     * 获取同比 或 环比
     */
    public static function getChain($data, $lastData, $precision = 0)
    {
        if (($data - $lastData) == 0) {
            return 0;
        }
        return $data > 0 ? ($lastData > 0 ? round(($data - $lastData) / $lastData * 100, $precision) : 100) : -100;
    }

    /**
     * @param $num
     * @return int
     * 转int
     */
    public static function toInt($num)
    {
        return !empty($num) ? intval($num) : 0;
    }


    /**
     * @param $value
     * @return int
     */
    public static function isValue($value)
    {
        return !empty($value) ? $value : 0;
    }


    public static function birthday($birthday, $now = '')
    {
        $age = strtotime($birthday);
        if ($age === false) {
            return false;
        }
        list($y1, $m1, $d1) = explode("-", date("Y-m-d", $age));
        if (empty($now)) {
            $now = strtotime("now");
        } else {
            $now = strtotime($now);
        }
        list($y2, $m2, $d2) = explode("-", date("Y-m-d", $now));
        $age = $y2 - $y1;
        if ((int)($m2 . $d2) < (int)($m1 . $d1))
            $age -= 1;
        return $age;
    }


    /**
     * @param $data
     * @param $lastData
     * @return int
     * 返回的  为1则上升，为-1下降，为0无变化
     */
    public static function getDataPk($data, $lastData)
    {
        if (($data - $lastData) == 0) {
            return 0;
        }
        return $data > $lastData ? 1 : -1;
    }

    /**
     * @param $array
     * @param $field
     * @return array
     * 二维数组去重
     */
    public static function uniquArr($array, $field)
    {
        $result = array();
        foreach ($array as $k => $val) {
            $code = false;
            foreach ($result as $_val) {
                if ($_val[$field] == $val[$field]) {
                    $code = true;
                    break;
                }
            }
            if (!$code) {
                $result[] = $val;
            }
        }
        return $result;
    }

    /**
     * @param $lng_lat
     * @param string $sign
     * @return array
     * 经纬度 转数组
     */
    public static function lngLatChange($lng_lat, $sign = ',')
    {
        $arr = explode($sign, $lng_lat);
        if (count($arr) == 2) {
            return $arr;
        }
        return [0, 0];
    }

    /**
     * @param $value
     * @return mixed
     * 去掉负号
     */
    public static function RemoveMinus($value)
    {
        return str_replace("-", "", $value);
    }

    /**
     * @param $number
     * @return mixed|string
     * 小写数字转成大写
     */
    public static function exchange($number){
        $number=substr($number,0,2);
        $arr=array("零","一","二","三","四","五","六","七","八","九");
        if(strlen($number)==1){
            $result=$arr[$number];
        }
        else{
            if($number==10){
                $result="十";
            }else{
                if($number<20){
                    $result="十";
                }else{
                    $result=$arr[substr($number,0,1)]."十";
                }
                if(substr($number,1,1)!="0"){
                    $result.=$arr[substr($number,1,1)];
                }
            }
        }
        return $result;
    }

    /**
     * @param $value
     * @return mixed
     * 小于0 的补充0
     */
    public static function SupplyZero($value)
    {
        if($value< 10 && $value > 0){
            return '0'.$value;
        }elseif ($value ==0){
            return 00;
        }
        return $value;
    }
}